package com.snap.vseries.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.SwitchCompat;
import android.view.View;

import com.snap.vseries.R;
import com.snap.vseries.base.BaseFragment;
import com.snap.vseries.bean.AnalysisProtocol;
import com.snap.vseries.bean.ExpInfo;
import com.snap.vseries.dialog.BaselineDialog;
import com.snap.vseries.dialog.NumberInputDialog;
import com.snap.vseries.ui.widget.TextButton;

import java.util.List;
import java.util.Locale;

import butterknife.BindViews;

@SuppressLint("ValidFragment")
public class InnerCTFragment extends BaseFragment {

    @BindViews({R.id.channel1_threshold_sw, R.id.channel2_threshold_sw, R.id.channel3_threshold_sw, R.id.channel4_threshold_sw})
    SwitchCompat[] thresholdSws;
    @BindViews({R.id.channel1_threshold_tv, R.id.channel2_threshold_tv, R.id.channel3_threshold_tv, R.id.channel4_threshold_tv})
    TextButton[] thresholdTvs;
    @BindViews({R.id.channel1_baseline_sw, R.id.channel2_baseline_sw, R.id.channel3_baseline_sw, R.id.channel4_baseline_sw,})
    SwitchCompat[] baselineSws;
    @BindViews({R.id.channel1_baseline_tv, R.id.channel2_baseline_tv, R.id.channel3_baseline_tv, R.id.channel4_baseline_tv,})
    TextButton[] baselineTvs;

    private ExpInfo experimentInfo;
    private List<AnalysisProtocol> protocols;
    private int maxBaseline;

    public InnerCTFragment(ExpInfo experimentInfo) {
        this.experimentInfo = experimentInfo;
        this.protocols = experimentInfo.getProtocols();
        this.maxBaseline = experimentInfo.getRunCycle();
    }

    @Override
    protected int layoutResId() {
        return R.layout.dialog_inner_ct;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        for (int i = 0; i < protocols.size(); i++) {
            final int index = i;
            AnalysisProtocol protocol = protocols.get(index);

            update(protocol.isAutoThreshold(), protocol.getThreshold(), protocol.isAutoBaseline(), protocol.getBaselineStart(), protocol.getBaselineStop(), index);

            thresholdSws[i].setOnCheckedChangeListener((compoundButton, b) -> updateSw(b, baselineSws[index].isChecked(), index));
            baselineSws[i].setOnCheckedChangeListener((compoundButton, b) -> updateSw(thresholdSws[index].isChecked(), b, index));
            thresholdTvs[i].setOnClickListener(v -> {
                new NumberInputDialog()
                        .setParam(3, 0, Float.parseFloat(thresholdTvs[index].getText().toString()), 5, "阈值", "",
                                value -> thresholdTvs[index].setText(String.valueOf(value)))
                        .setMaxLength(5)
                        .show(getFragmentManager(), "");
            });
            baselineTvs[index].setOnClickListener(v -> {
                String[] s = baselineTvs[index].getText().toString().split(" ~ ");
                int blStart = Integer.parseInt(s[0]);
                int blStop = Integer.parseInt(s[1]);
                new BaselineDialog()
                        .setParam(maxBaseline, blStart, blStop,
                                (start, stop) -> baselineTvs[index].setText(String.format(Locale.CHINA, "%d ~ %d", start, stop)))
                        .show(getFragmentManager(), "");
            });
        }
    }

    public void apply() {
        for (int i = 0; i < protocols.size(); i++) {
            protocols.get(i).setAutoThreshold(thresholdSws[i].isChecked());
            protocols.get(i).setThreshold(Float.parseFloat(thresholdTvs[i].getText().toString()));
            protocols.get(i).setAutoBaseline(baselineSws[i].isChecked());
            String[] s = baselineTvs[i].getText().toString().split(" ~ ");
            int blStart = Integer.parseInt(s[0]);
            int blStop = Integer.parseInt(s[1]);
            protocols.get(i).setBaselineStart(blStart);
            protocols.get(i).setBaselineStop(blStop);
        }
    }

    private void update(boolean autoThreshold, float threshold, boolean autoBaseline, int baselineStart, int baselineStop, int index) {
        if (autoThreshold) autoBaseline = true;
        thresholdSws[index].setChecked(autoThreshold);
        thresholdTvs[index].setText(String.valueOf(threshold));
        baselineSws[index].setChecked(autoBaseline);
        baselineTvs[index].setText(String.format(Locale.CHINA, "%d ~ %d", baselineStart, baselineStop));
        if (autoThreshold) {
            thresholdTvs[index].setVisibility(View.INVISIBLE);
            baselineTvs[index].setVisibility(View.INVISIBLE);
            baselineSws[index].setEnabled(false);
        } else {
            thresholdTvs[index].setVisibility(View.VISIBLE);
            baselineSws[index].setEnabled(true);
            if (autoBaseline) {
                baselineTvs[index].setVisibility(View.INVISIBLE);
            } else {
                baselineTvs[index].setVisibility(View.VISIBLE);
            }
        }
    }

    private void updateSw(boolean autoThreshold, boolean autoBaseline, int index) {
        if (autoThreshold) autoBaseline = true;
        thresholdSws[index].setChecked(autoThreshold);
        baselineSws[index].setChecked(autoBaseline);

        if (autoThreshold) {
            thresholdTvs[index].setVisibility(View.INVISIBLE);
            baselineTvs[index].setVisibility(View.INVISIBLE);
            baselineSws[index].setEnabled(false);
        } else {
            thresholdTvs[index].setVisibility(View.VISIBLE);
            baselineSws[index].setEnabled(true);
            if (autoBaseline) {
                baselineTvs[index].setVisibility(View.INVISIBLE);
            } else {
                baselineTvs[index].setVisibility(View.VISIBLE);

            }
        }
    }
}
